package cn.kmsoft.common.utils;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

import java.io.*;
import java.util.Enumeration;

public class ZipUtil {
    private static int buffer = 2048;

    /**
     * 压缩文件
     *
     * @param path
     * @param names
     * @param dest
     * @throws IOException
     */
    public static void zip(String path, String[] names, String dest) throws IOException {
        zip(path, names, dest, "utf-8");
    }

    /**
     * 压缩文件
     *
     * @param path     原地址
     * @param names    文件名称
     * @param dest     目标地址
     * @param encoding
     * @throws IOException
     */
    public static void zip(String path, String[] names, String dest, String encoding) throws IOException {
        ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));
        zos.setEncoding(encoding);
        byte data[] = new byte[buffer];
        int len;

        for (int i = 0; i < names.length; i++) {
            ZipEntry entry = new ZipEntry(names[i]);
            zos.putNextEntry(entry);
            FileInputStream fis = new FileInputStream(path + File.separator + names[i]);

            while ((len = fis.read(data, 0, buffer)) != -1) {
                zos.write(data, 0, len);
            }
            fis.close();
            zos.closeEntry();
        }
        zos.close();
    }

    public static void zip(String path, String workdir, String name, String[] exts) throws IOException {
        FileOutputStream zfos = new FileOutputStream(path + workdir + "\\" + name + ".zip");

        java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(new BufferedOutputStream(zfos));
        BufferedInputStream bis = null;
        int count;
        int BUFFER = 2048;
        byte data[] = new byte[BUFFER];

        for (int i = 0; i < exts.length; i++) {
            java.util.zip.ZipEntry entry = new java.util.zip.ZipEntry("data." + exts[i]);
            zos.putNextEntry(entry);
            FileInputStream fis = new FileInputStream(path + "\\" + workdir + "\\" + name + "." + exts[i]);
            bis = new BufferedInputStream(fis, BUFFER);

            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                zos.write(data, 0, count);
            }
            bis.close();
        }
        zos.close();
    }

    /**
     * 解压文件
     *
     * @param file
     * @param dest
     * @throws IOException
     */
    public static void unzip(String file, String dest) throws IOException {
        ZipFile zip = new ZipFile(file);
        Enumeration<ZipEntry> entries = zip.getEntries();
        byte data[] = new byte[buffer];
        int len;

        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File item = new File(dest + File.separator + entry.getName());

            if (entry.isDirectory()) {
                item.mkdirs();
            } else {
                InputStream stream = zip.getInputStream(entry);
                FileOutputStream fos = new FileOutputStream(item);

                while ((len = stream.read(data, 0, buffer)) != -1) {
                    fos.write(data, 0, len);
                }
                fos.close();
                stream.close();
            }
            entry.clone();
        }
        zip.close();
    }

    public static void unzip(String file, String dest, String newName) throws IOException {
        ZipFile zip = new ZipFile(file);
        Enumeration<ZipEntry> entries = zip.getEntries();
        byte data[] = new byte[buffer];
        int len;

        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            String name = entry.getName();
            String ext = name.substring(name.lastIndexOf("."));
            File item = new File(dest + File.separator + newName + ext);

            if (entry.isDirectory()) {
                item.mkdirs();
            } else {
                InputStream stream = zip.getInputStream(entry);
                FileOutputStream fos = new FileOutputStream(item);

                while ((len = stream.read(data, 0, buffer)) != -1) {
                    fos.write(data, 0, len);
                }
                fos.close();
                stream.close();
            }
            entry.clone();
        }
        zip.close();
    }
}
